export default {
  namespaced: true,
  state: {
    reloadComponent: null,
    visitedViews: []
  },
  getters: {
    cachedViews(state) {
      let arr = []
      if (state.reloadComponent) {
        arr = state.visitedViews.filter(i => i.menuId !== state.reloadComponent.menuId && i.meta && !i.meta.noCache).map(i => i.name)
      } else {
        arr = state.visitedViews.filter(i => i.meta && !i.meta.noCache).map(i => i.name)
      }
      return arr
    }
  },
  mutations: {
    addVisitedView(state, view) {
      if (!view || state.visitedViews.some(v => v.meta.menuId === view.meta.menuId)) return
      state.visitedViews.push(view)
    },
    removeTab(state, id) {
      const index = state.visitedViews.findIndex(i => i.meta.menuId === id)
      state.visitedViews.splice(index, 1)
    },
    removeLeft(state, id) {
      const index = state.visitedViews.findIndex(i => i.meta.menuId === id)
      state.visitedViews = state.visitedViews.slice(index)
    },
    removeRight(state, id) {
      const index = state.visitedViews.findIndex(i => i.meta.menuId === id)
      state.visitedViews = state.visitedViews.slice(0, index + 1)
    },
    removeOther(state, id) {
      const index = state.visitedViews.findIndex(i => i.meta.menuId === id)
      state.visitedViews = [state.visitedViews[index]]
    },
    removeAll(state) {
      state.visitedViews = []
    }
  },
  actions: {
    setReloadComponent({ state }, view) {
      state.reloadComponent = view
    }
  }
}
